﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: DatCompanyInfo.cs
// 作 者：代码自动生成
// 日 期：2011-10-26 11:07:14
// 描 述：
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using Csla;
using lite;
using Csla.Validation;
using Csla.Data;
using System.Data.SqlClient;
using ZX.DAL;
using OpenExpressApp.ORM;
using System.Reflection;
using System.Text.RegularExpressions;

namespace ZX.EHR.Library
{
	/// <summary>
    /// 
    /// </summary>
	[Serializable]
    [Table(Name="DatCompanyInfo")]
    [ZX.DBModule.Table("DatCompanyInfo","CompanyID")]
	public class DatCompanyInfo: ZX.DBModule.BaseEntity
	{
        
        #region Business Methods 业务方法
       
		/// <summary>
        /// 公司ID
        /// </summary>
        private static PropertyInfo< Guid > CompanyIDProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< Guid >("CompanyID"));
        [DataObjectField(true, true)]
        [Column, PK]
        public Guid CompanyID
        {
            get { return GetProperty(CompanyIDProperty); }
            set { SetProperty(CompanyIDProperty, value); }
        }
        
		/// <summary>
        /// 公司父节点
        /// </summary>
        private static PropertyInfo< Guid > ParentIDProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< Guid >("ParentID"));
        [Column]
        public Guid ParentID
        {
            get { return GetProperty(ParentIDProperty); }
            set { SetProperty(ParentIDProperty, value); }
        }
		/// <summary>
        /// 公司名称
        /// </summary>
        private static PropertyInfo< string > CompanyNameProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("CompanyName"));
        [Column]
        public string CompanyName
        {
            get { return GetProperty(CompanyNameProperty); }
            set { SetProperty(CompanyNameProperty, value); }
        }
		/// <summary>
        /// 公司助记编号
        /// </summary>
        private static PropertyInfo< string > HelpCodeProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("HelpCode"));
        [Column]
        public string HelpCode
        {
            get { return GetProperty(HelpCodeProperty); }
            set { SetProperty(HelpCodeProperty, value); }
        }
		/// <summary>
        /// 公司类型  S销售公司 P生产公司 C软件公司 A加工公司.J工程項目公司 D軟件代工公司.
        /// </summary>
        private static PropertyInfo< string > CTypeProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("CType"));
        [Column]
        public string CType
        {
            get { return GetProperty(CTypeProperty); }
            set { SetProperty(CTypeProperty, value); }
        }
		/// <summary>
        /// 公司英文名
        /// </summary>
        private static PropertyInfo< string > EnNameProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("EnName"));
        [Column]
        public string EnName
        {
            get { return GetProperty(EnNameProperty); }
            set { SetProperty(EnNameProperty, value); }
        }
		/// <summary>
        /// 电话号码
        /// </summary>
        private static PropertyInfo< string > PhoneProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Phone"));
        [Column]
        public string Phone
        {
            get { return GetProperty(PhoneProperty); }
            set { SetProperty(PhoneProperty, value); }
        }
		/// <summary>
        /// 传真
        /// </summary>
        private static PropertyInfo< string > FaxProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Fax"));
        [Column]
        public string Fax
        {
            get { return GetProperty(FaxProperty); }
            set { SetProperty(FaxProperty, value); }
        }
		/// <summary>
        /// 电子邮件
        /// </summary>
        private static PropertyInfo< string > EmailProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Email"));
        [Column]
        public string Email
        {
            get { return GetProperty(EmailProperty); }
            set { SetProperty(EmailProperty, value); }
        }
		/// <summary>
        /// 公司企业网站
        /// </summary>
        private static PropertyInfo< string > URLProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("URL"));
        [Column]
        public string URL
        {
            get { return GetProperty(URLProperty); }
            set { SetProperty(URLProperty, value); }
        }
		/// <summary>
        /// 公司地址
        /// </summary>
        private static PropertyInfo< string > AddressProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Address"));
        [Column]
        public string Address
        {
            get { return GetProperty(AddressProperty); }
            set { SetProperty(AddressProperty, value); }
        }
		/// <summary>
        /// 子公司0,总公司1
        /// </summary>
        private static PropertyInfo< bool > IsParentProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< bool >("IsParent"));
        [Column]
        public bool IsParent
        {
            get { return GetProperty(IsParentProperty); }
            set { SetProperty(IsParentProperty, value); }
        }
		/// <summary>
        /// 状态(OP运行中;CA停用)
        /// </summary>
        private static PropertyInfo< string > StatusProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Status"));
        [Column]
        public string Status
        {
            get { return GetProperty(StatusProperty); }
            set { SetProperty(StatusProperty, value); }
        }
		/// <summary>
        /// 法人代表
        /// </summary>
        private static PropertyInfo< string > ArtificialPersonProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("ArtificialPerson"));
        [Column]
        public string ArtificialPerson
        {
            get { return GetProperty(ArtificialPersonProperty); }
            set { SetProperty(ArtificialPersonProperty, value); }
        }
		/// <summary>
        /// 所属行业
        /// </summary>
        private static PropertyInfo< string > VocationProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Vocation"));
        [Column]
        public string Vocation
        {
            get { return GetProperty(VocationProperty); }
            set { SetProperty(VocationProperty, value); }
        }
		/// <summary>
        /// 经济类型ID
        /// </summary>
        private static PropertyInfo< Guid > EconomyTypeProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< Guid >("EconomyType"));
        [Column]
        public Guid EconomyType
        {
            get { return GetProperty(EconomyTypeProperty); }
            set { SetProperty(EconomyTypeProperty, value); }
        }
		/// <summary>
        /// 是否有效
        /// </summary>
        private static PropertyInfo< bool > IsValueProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< bool >("IsValue"));
        [Column]
        public bool IsValue
        {
            get { return GetProperty(IsValueProperty); }
            set { SetProperty(IsValueProperty, value); }
        }
        /// <summary>
        /// 邮政编码
        /// </summary>
        private static PropertyInfo<int> PostCodeProperty = RegisterProperty(typeof(DatCompanyInfo), new PropertyInfo<int>("PostCode"));
        [Column]
        public int PostCode
        {
            get { return GetProperty(PostCodeProperty); }
            set { SetProperty(PostCodeProperty, value); }
        }
        
		/// <summary>
        /// 说明
        /// </summary>
        private static PropertyInfo< string > DescriptionProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("Description"));
        [Column]
        public string Description
        {
            get { return GetProperty(DescriptionProperty); }
            set { SetProperty(DescriptionProperty, value); }
        }
		/// <summary>
        /// 分司路径,分隔符为"." 例如001.001.002[3位]
        /// </summary>
        private static PropertyInfo< string > CompanyPathProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< string >("CompanyPath"));
        [Column]
        public string CompanyPath
        {
            get { return GetProperty(CompanyPathProperty); }
            set { SetProperty(CompanyPathProperty, value); }
        }
		/// <summary>
        /// 创建日
        /// </summary>
        private static PropertyInfo< DateTime > CreateDateProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< DateTime >("CreateDate"));
        [Column]
        public DateTime CreateDate
        {
            get { return GetProperty(CreateDateProperty); }
            set { SetProperty(CreateDateProperty, value); }
        }
		/// <summary>
        /// 创建人
        /// </summary>
        private static PropertyInfo< Guid > CreateUserProperty = RegisterProperty(typeof(DatCompanyInfo),new PropertyInfo< Guid >("CreateUser"));
        [Column]
        public Guid CreateUser
        {
            get { return GetProperty(CreateUserProperty); }
            set { SetProperty(CreateUserProperty, value); }
        }
		
        #endregion
        
        #region Validation Rules 验证规则
        //将验证规则与业务对象的属性联系在一起
        protected override void AddBusinessRules()
        {
            //AddRule的第一个参数是委托变量，也就是说是方法的变量
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyName", 100));
            ValidationRules.AddRule(CommonRules.StringRequired, "HelpCode");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("HelpCode", 40));
            ValidationRules.AddRule(CommonRules.StringRequired, "CType");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CType", 1));
            ValidationRules.AddRule(CommonRules.StringRequired, "EnName");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("EnName", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "Phone");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Phone", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Fax");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Fax", 30));
            ValidationRules.AddRule(CommonRules.StringRequired, "Email");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Email", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "URL");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("URL", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "Address");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Address", 50));
            ValidationRules.AddRule(CommonRules.StringRequired, "Status");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Status", 10));
            ValidationRules.AddRule(CommonRules.StringRequired, "ArtificialPerson");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("ArtificialPerson", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "Vocation");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Vocation", 20));
            ValidationRules.AddRule(CommonRules.StringRequired, "Description");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("Description", 150));
            ValidationRules.AddRule(CommonRules.StringRequired, "CompanyPath");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new Csla.Validation.CommonRules.MaxLengthRuleArgs("CompanyPath", 50));
            //base.AddBusinessRules();
        }
        #endregion
        
        #region Factory Methods 工厂方法

        public DatCompanyInfo(){}

        public static DatCompanyInfo New()
        {
            return DataPortal.Create< DatCompanyInfo>();
        }
        public static DatCompanyInfo Get(Guid CompanyID)
        {
            return DataPortal.Fetch< DatCompanyInfo>(new SingleLazyCriteria< DatCompanyInfo, Guid>(CompanyID, false));
        }
        public static void Delete(Guid CompanyID)
        {
            DataPortal.Delete(CompanyID);
        }

        //public override DatCompanyInfo Save()
        //{
            //return base.Save();
        //}
        #endregion
		
		#region Data Access 数据访问
        //由于DataPortal_create中没有对数据库的访问，所有标记了Runlocal特性，使得该方法只需在本地运行即可
        //如果代码中有访问数据库的方法，那么就不要用这个特性，使数据门户可以根据情况将此方法的运行选择在应用服务器还是本地
        [RunLocal()]
        protected override void DataPortal_Create()
        {
            //在此调用CheckRules对所有的属性进行验证规则的检查，这样检查只运行一次。
            ValidationRules.CheckRules();
        }


        //可以看到此方法没有标记任何特性
        //此方法要访问数据库所以不能标记本地，由于她没有任何数据库更新操作所以也无需事务性的保护
        //此方法调用结束后会隐含的调用MarkOld（）方法
        private void DataPortal_Fetch(Guid CompanyID)
        {
            //通过连接字符串建立SQL连接对象，Using符号代表，范围释放的意思，SqlConnection对象会在此范围后被释放掉
             using (var ctx = ConnectionManager<SqlConnection>.GetManager(ConnectionStringNames))
            {
                IDb db = DbFactory.Instance.GetDb(ctx.Connection);
                IQuery q = db.Query();
                q.Constrain("CompanyID").Equal(CompanyID);
                var data = db.Select< DatCompanyInfo>(q)[0];
                LoadProperty(CompanyIDProperty, data.CompanyID);
                LoadProperty(ParentIDProperty, data.ParentID);
                LoadProperty(CompanyNameProperty, data.CompanyName);
                LoadProperty(HelpCodeProperty, data.HelpCode);
                LoadProperty(CTypeProperty, data.CType);
                LoadProperty(EnNameProperty, data.EnName);
                LoadProperty(PhoneProperty, data.Phone);
                LoadProperty(FaxProperty, data.Fax);
                LoadProperty(EmailProperty, data.Email);
                LoadProperty(URLProperty, data.URL);
                LoadProperty(AddressProperty, data.Address);
                LoadProperty(IsParentProperty, data.IsParent);
                LoadProperty(StatusProperty, data.Status);
                LoadProperty(ArtificialPersonProperty, data.ArtificialPerson);
                LoadProperty(VocationProperty, data.Vocation);
                LoadProperty(EconomyTypeProperty, data.EconomyType);
                LoadProperty(IsValueProperty, data.IsValue);
                LoadProperty(DescriptionProperty, data.Description);
                LoadProperty(CompanyPathProperty, data.CompanyPath);
                LoadProperty(CreateDateProperty, data.CreateDate);
                LoadProperty(CreateUserProperty, data.CreateUser);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            //依然是调用下面的方法删除的，只是他此时可以获得自己的Id
            DataPortal_Delete(CompanyIDProperty);
        }
        public override string CheckData()
        {
            if (this.CompanyName==null||this.CompanyName.Trim()=="")
            {
                return "公司名称不能为空";
            }
            if (this.Address == null || this.Address.Trim() == "")
            {
                return "公司地址不能为空";
            }
            if (this.Phone == null || this.Phone.Trim() == "")
            {
                return "公司电话不能为空";
            }
            if (this.URL == null || this.URL.Trim() == "")
            {
                return "请输入正确有效的网站地址";
            }
            if (!Regex.IsMatch(this.Email, @"^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,3})\s*$"))
            {
                return "邮箱格式不合法!";
            }
            return base.CheckData();
        }
        #endregion

	}
}

